//
//  CreateJobReviewInstallAddressView.swift
//  GreenDeal
//
//  Created by Moennig Q on 2023/3/7.
//

import UIKit
import SnapKit
class CreateJobReviewInstallAddressView: View {
    let titleView = CreateJobReviewSectionView(R.string.localizable.address().uppercased(),haveEditBtnFlag: true)
    let installAddressText = CreateJobReviewContentLabel()
    //
    let specialAddressTitle = CreateJobReviewTitleLabel()
    let specialAddressText = CreateJobReviewContentLabel()
    //
    let postalAddressText = CreateJobReviewContentLabel()
    override func setUpView() {
        //titleView
        self.addSubview(titleView)
        titleView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
        }
        //install address
        let installAddressTitle = CreateJobReviewTitleLabel(R.string.localizable.installationAddress())
        //抗压缩能力优先级高
        installAddressTitle.setContentCompressionResistancePriority(.required, for: NSLayoutConstraint.Axis.horizontal)
        self.addSubview(installAddressTitle)
        installAddressTitle.snp.makeConstraints { make in
            make.top.equalTo(titleView.snp.bottom).offset(16)
            make.left.equalTo(16)
            make.height.width.greaterThanOrEqualTo(0)
        }
        //
        installAddressText.numberOfLines = 0
        installAddressText.textAlignment = .right
        self.addSubview(installAddressText)
        installAddressText.snp.makeConstraints { make in
            make.left.equalTo(installAddressTitle.snp.right).offset(5)
            make.top.equalTo(installAddressTitle)
            make.right.equalTo(-16)
            make.height.greaterThanOrEqualTo(installAddressTitle)
        }
        //special address
        specialAddressTitle.text = R.string.localizable.specialAddress()
        //抗压缩能力优先级高
        specialAddressTitle.setContentCompressionResistancePriority(.required, for: NSLayoutConstraint.Axis.horizontal)
        self.addSubview(specialAddressTitle)
        specialAddressTitle.snp.makeConstraints { make in
            make.top.equalTo(installAddressText.snp.bottom).offset(CREATEJOB_REVIEW_INTERNAL)
            make.left.equalTo(installAddressTitle)
            make.height.width.greaterThanOrEqualTo(0)
        }
        //
        specialAddressText.numberOfLines = 0
        specialAddressText.textAlignment = .right
        self.addSubview(specialAddressText)
        specialAddressText.snp.makeConstraints { make in
            make.left.equalTo(specialAddressTitle.snp.right).offset(5)
            make.top.equalTo(specialAddressTitle)
            make.right.equalTo(installAddressText)
            make.height.greaterThanOrEqualTo(specialAddressTitle)
        }
        //postal address
        let postalAddressTitle = CreateJobReviewTitleLabel(R.string.localizable.postalAddress())
        //抗压缩能力优先级高
        postalAddressTitle.setContentCompressionResistancePriority(.required, for: NSLayoutConstraint.Axis.horizontal)
        self.addSubview(postalAddressTitle)
        postalAddressTitle.snp.makeConstraints { make in
            make.top.equalTo(specialAddressText.snp.bottom).offset(CREATEJOB_REVIEW_INTERNAL)
            make.left.equalTo(installAddressTitle)
            make.height.width.greaterThanOrEqualTo(0)
        }
        //
        postalAddressText.numberOfLines = 0
        postalAddressText.textAlignment = .right
        self.addSubview(postalAddressText)
        postalAddressText.snp.makeConstraints { make in
            make.left.equalTo(postalAddressTitle.snp.right).offset(5)
            make.top.equalTo(postalAddressTitle)
            make.right.equalTo(installAddressText)
            make.height.greaterThanOrEqualTo(postalAddressTitle)
            make.bottom.equalTo(-16)
        }
    }
    func setData(){
        self.installAddressText.text = CreateJobModelHelper.shared.createJobViewModel.installationAddressViewModel.installAddress.full_address
        //又增加special address
        if CreateJobModelHelper.shared.createJobViewModel.installationAddressViewModel.addSpecialAddressFlag {//有special address
            specialAddressTitle.isHidden = false
            specialAddressText.isHidden = false
            specialAddressTitle.snp.remakeConstraints { make in
                make.top.equalTo(installAddressText.snp.bottom).offset(CREATEJOB_REVIEW_INTERNAL)
                make.left.equalTo(16)
                make.height.width.greaterThanOrEqualTo(0)
            }
            
            self.specialAddressText.text = CreateJobModelHelper.shared.createJobViewModel.installationAddressViewModel.specialAddress
        }else{//无special address
            specialAddressTitle.isHidden = true
            specialAddressText.isHidden = true
            specialAddressTitle.snp.remakeConstraints { make in
                make.edges.equalTo(installAddressText)
            }
            self.specialAddressText.text = ""
        }
        //和安装地址一样
        if CreateJobModelHelper.shared.createJobViewModel.installationAddressViewModel.sameAsPostalAddress {
            self.postalAddressText.text = CreateJobModelHelper.shared.createJobViewModel.installationAddressViewModel.installAddress.full_address
        }else{//不一样
            let addressType = CreateJobModelHelper.shared.createJobViewModel.installationAddressViewModel.postalAddress.address_type
            if addressType == .physical {//
                self.postalAddressText.text = CreateJobModelHelper.shared.createJobViewModel.installationAddressViewModel.postalAddress.physicalAddress.full_address
            }else if addressType == .poBox {//
                self.postalAddressText.text = CreateJobModelHelper.shared.createJobViewModel.installationAddressViewModel.postalAddress.po_box_full_address
            }
        }
    }
}
